package services

import (
	"context"
	"database/sql"
	"fmt"
	"navidog/backend/types"

	_ "github.com/go-sql-driver/mysql"
)

type MysqlService struct {
	ctx context.Context
}

func NewMysqlService() *MysqlService {
	return &MysqlService{}
}

func (m *MysqlService) StartUp(ctx context.Context) {
	m.ctx = ctx
}

func (m *MysqlService) TestConnection(connection types.Connection) types.JSResp {
	db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", connection.Config.User, connection.Config.Password, connection.Config.Host, connection.Config.Port, connection.Config.Database))
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("连接失败: %s", err.Error()),
		}
	}
	defer db.Close()
	err = db.Ping()
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("连接失败: %s", err.Error()),
		}
	}
	return types.JSResp{
		Success: true,
		Msg:     "连接成功",
		Data:    nil,
	}
}

func (m *MysqlService) ListDatabases(uuid string) types.JSResp {
	if uuid == "" {
		return types.JSResp{
			Success: true,
			Msg:     "连接UUID不能为空",
			Data:    []string{},
		}
	}
	connectionService := NewConnectionService()
	connection, err := connectionService.getConnection(uuid)
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("加载连接失败: %s", err.Error()),
		}
	}
	db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/", connection.Config.User, connection.Config.Password, connection.Config.Host, connection.Config.Port))
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("连接失败: %s", err.Error()),
		}
	}
	defer db.Close()
	rows, err := db.Query("SHOW DATABASES")
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("查询失败: %s", err.Error()),
		}
	}
	defer rows.Close()
	databases := []string{}
	for rows.Next() {
		var database string
		err = rows.Scan(&database)
		if err != nil {
			return types.JSResp{
				Success: false,
				Msg:     fmt.Sprintf("查询失败: %s", err.Error()),
			}
		}
		databases = append(databases, database)
	}
	return types.JSResp{
		Success: true,
		Msg:     "查询成功",
		Data:    databases,
	}
}

func (m *MysqlService) ListTables(uuid string, database string) types.JSResp {
	if uuid == "" {
		return types.JSResp{
			Success: false,
			Msg:     "连接UUID不能为空",
			Data:    []string{},
		}
	}
	if database == "" {
		return types.JSResp{
			Success: false,
			Msg:     "数据库名称不能为空",
			Data:    []string{},
		}
	}
	connectionService := NewConnectionService()
	connection, err := connectionService.getConnection(uuid)
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("加载连接失败: %s", err.Error()),
		}
	}
	db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", connection.Config.User, connection.Config.Password, connection.Config.Host, connection.Config.Port, database))
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("连接失败: %s", err.Error()),
		}
	}
	defer db.Close()
	rows, err := db.Query("SHOW TABLES")
	if err != nil {
		return types.JSResp{
			Success: false,
			Msg:     fmt.Sprintf("查询失败: %s", err.Error()),
		}
	}
	defer rows.Close()
	tables := []string{}
	for rows.Next() {
		var table string
		err = rows.Scan(&table)
		if err != nil {
			return types.JSResp{
				Success: false,
				Msg:     fmt.Sprintf("查询失败: %s", err.Error()),
			}
		}
		tables = append(tables, table)
	}
	return types.JSResp{
		Success: true,
		Msg:     "查询成功",
		Data:    tables,
	}
}
